package com.huizhonglm.diap.core.upgrade;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.utils.ToastUtil;

import java.io.File;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-06-24 21:18
 */
public class NotificationReceiver extends BroadcastReceiver {

    private NotificationManager nManager;

    private NotificationCompat.Builder nBuilder;

    private String mode = "0";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction() != null) {
            if (nManager == null) {
                nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            }
            if (nBuilder == null) {
                nBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher);
            }
            handlerAction(context, intent);
        }
    }

    private void handlerAction(Context context, Intent intent){
        mode = intent.getStringExtra("mode");
        int notificationID = intent.getIntExtra("notificationID", 99999);

        switch (intent.getAction()) {
            case Constants.UPGRADE_DOWNLOADER_SERVICE_START:
                ToastUtil.makeTextShort("正在后台下载");
                nBuilder.setContentTitle("新版安装包下载中...");
                break;
            case Constants.UPGRADE_DOWNLOADER_SERVICE_PROGRESS:
                float fileSize = intent.getFloatExtra("fileSize", 0f);
                float compSize = intent.getFloatExtra("compSize", 0f);
                nBuilder.setProgress((int) fileSize, (int) compSize, false);
                float percent = (compSize / fileSize) * 100.0f;
                nBuilder.setContentText("新版安装包已下载" + (int) percent +"%");
                nManager.notify(notificationID, nBuilder.build());
                break;
            case Constants.UPGRADE_DOWNLOADER_SERVICE_FINISH:
                ToastUtil.makeTextShort("下载完成");
                String localPath = intent.getStringExtra("localPath");
                nManager.cancel(notificationID);
                File apk = new File(localPath);
                installApk(context, apk);
                break;
            case Constants.UPGRADE_DOWNLOADER_SERVICE_ERROR:
                ToastUtil.makeTextShort("下载失败，请检查网络连接");
                nManager.cancel(notificationID);
                break;
        }
    }

    private void installApk(Context context, File apkFile){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        context.startActivity(intent);
        if ("2".equals(mode)) {
            context.sendBroadcast(new Intent(Constants.HUIZHONGLM_EXIT_ACITON));
        }
    }
}
